<?php
/**
 * Created by PhpStorm
 * User: 王帆
 * Date: 2020-07-13
 * Time: 10:58
 */
require('../phpMQTT.php');
$data = require('config.php');
$param_arr = getopt('t:k:');
if (!key_exists('t',$param_arr) || !key_exists('k',$param_arr)){
    echo "缺少参数!\n";die();
}
$t = $param_arr['t'];
$k = $param_arr['k'];
if (key_exists($t,$data['topicArray'])){
    $mqtt = new Bluerhinos\phpMQTT($data['config']['server'], $data['config']['port'], $data['config']['publisher_client_id']);
    if ($mqtt->connect(true, NULL, $data['config']['username'], $data['config']['password'])) {
        $topic = $data['topicArray'][$t];
        $pubData = [
            'time' => date('Y-m-d H:i:s'),
            'name' => $topic['name'],
            'k' => $k
        ];
        $mqtt->publish($topic['key'], json_encode($pubData), 0, false);
        $mqtt->close();
    } else {
        echo "连接超时!\n";die();
    }
}else {
    echo "专题不存在!\n";die();
}